//
//  LDCMyPatientViewController.m
//  LaiKeDaoClient
//
//  Created by zouzhisheng on 15/9/28.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "LDCMyPatientViewController.h"
#import "KDMypatientTableViewCell.h"
#import "KDPatientDataViewController.h"
#import "KDChooseViewController.h"
@implementation MyPatientNavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor clearColor];
    LDCMyPatientViewController *patientVC=[[LDCMyPatientViewController alloc] init];
    self.viewControllers = @[patientVC];
}

@end



@interface LDCMyPatientViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,KDChooseVCDelegate>{
    UITableView                 *m_tableView;
}

@property(nonatomic,strong)NSArray      *dataArr;

@end

@implementation LDCMyPatientViewController

-(void)clickRightButton:(id)sender{
    KDChooseViewController  *controller = [[KDChooseViewController alloc] init];
    controller.chooseVCDeleagte = self;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviTitle:@"我的患者"];
    [self setNaviRightButton:@"筛选"];
    
    UISearchBar *searbar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 44)];
    searbar.placeholder = @"输入姓名、手机号查找患者";
    searbar.delegate = self;
    
    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49) style:UITableViewStylePlain];
    m_tableView.delegate =self;
    m_tableView.dataSource = self;
    m_tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:m_tableView];
    m_tableView.tableHeaderView = searbar;
    
    [self requestGetPatientListKeyword:nil date:nil sex:nil age:nil disease:nil solution:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self requestGetPatientListKeyword:searchBar.text date:nil sex:nil age:nil disease:nil solution:nil];
}
//choose Delegate
- (void)chooseResultSex:(NSString *)sex age:(NSString *)age disease:(NSString *)disease solution:(NSString *)solution{
    [self requestGetPatientListKeyword:nil date:nil sex:sex age:age disease:disease solution:solution];
}

//网络
//获取患者列表
- (void)requestGetPatientListKeyword:(NSString *)Keyword date:(NSString *)date sex:(NSString *)sex age:(NSString *)age disease:(NSString *)disease solution:(NSString *)solution
{
    
    [CSHApiClient getPatientListKeyword:Keyword date:date sex:sex age:age disease:disease solution:solution didFinishLoaded:^(LKDPatientListModel *model, NSString *path) {
        if (model.data.count > 0) {
            self.dataArr = model.data;
            [m_tableView reloadData];
        }else{
            [self showAlert:@"没有查到符合条件的患者"];
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
    }];
}
- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}
#pragma mark - tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 54;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LKDPatientListSubModel *list = [self.dataArr objectAtIndex:section];
    return list.list.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    KDPatientDataViewController *controller = [[KDPatientDataViewController alloc] init];
    LKDPatientListSubModel *list = [self.dataArr objectAtIndex:indexPath.section];
    controller.dataDic = [list.list objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* str=  @"cell";
    KDMypatientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: str];
    if (cell ==nil) {
        cell = [[KDMypatientTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        
        UIView  *line = [[UIView alloc] initWithFrame:(CGRect){0,53,kScreenWidth,1}];
        [line setBackgroundColor:[UIColor colorWithHexString:@"0xF2F2F2"]];
        [cell.contentView addSubview:line];
    }
    
    LKDPatientListSubModel *list = [self.dataArr objectAtIndex:indexPath.section];
    [cell reloadData:[list.list objectAtIndex:indexPath.row]];
    
    return cell;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        view.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
        
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor blackColor];
        label.text = [NSString stringWithFormat:@"  %@",((LKDPatientListSubModel *)[self.dataArr objectAtIndex:section]).group];
        [view addSubview:label];
        
        return view;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
